/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.UserType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class UsertypeDAO {

     private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<UserType> GetAll() {
        ArrayList<UserType> listUserType = new ArrayList<UserType>();
        UserType userType = null;
        try {

            strCommand = "select * from tbl_UserType";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
               userType=new UserType();
               userType.setUserTypeID(rs.getInt("userTypeID"));
               userType.setUserTypeName(rs.getString("userTypeName"));
               userType.setUserTypeStatus(rs.getInt("userTypeStatus"));
               
               listUserType.add(userType);
            }
            return listUserType;
        } catch (SQLException ex) {
            Logger.getLogger(UsertypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listUserType;
    }

    public boolean AddUserType(String userTypeName) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_UserType (userTypeName,userTypeStatus) VALUES (?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setString(1, userTypeName);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(UsertypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public UserType GetByID(int userTypeID) {

        UserType userType = new UserType();
        try {

            strCommand = "select * from tbl_UserType WHERE userTypeID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, userTypeID);
            rs = ps.executeQuery();
            while (rs.next()) {
               userType.setUserTypeID(rs.getInt("userTypeID"));
               userType.setUserTypeName(rs.getString("userTypeName"));
               userType.setUserTypeStatus(rs.getInt("userTypeStatus"));


            }
            return userType;
        } catch (SQLException ex) {
            Logger.getLogger(UsertypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return userType;
    }

    public boolean changeStatus(int userTypeID, int userTypeStatus) {
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_UserType SET userTypeStatus = ? WHERE userTypeID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, userTypeStatus);
            ps.setInt(2, userTypeID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean editUserType(UserType objUserType) {
       
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_UserType SET userTypeName = ?, userTypeStatus = ? WHERE userTypeID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, objUserType.getUserTypeName());
            ps.setInt(2, objUserType.getUserTypeStatus());
            ps.setInt(3, objUserType.getUserTypeID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteUserType(int userTypeID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_Banks WHERE userTypeID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, userTypeID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
